package TestLib;

use strict;
use warnings;
use diagnostics;
use utf8;
use DBI;
use English qw(-no_match_vars);
use Exporter qw(import);
use Readonly;

Readonly(our @EXPORT => qw(
    test_get_401
    test_get_401_badauth
));

Readonly(our @EXPORT_OK => qw(
    get_testuser_password
));

Readonly(our @PATHS => (
    '/',
    '/ahoj',
    '/ahoj/znova',
    '/result',
    '/result?action=save',
    '/archive',
    '/help',
    '/conf',
    '/conf?section=data&action=view',
    '/conf?section=data&action=manage',
    '/conf?section=user',
));

our $tup;
# testuser password

{
    my $p_f = "$ENV{SKAL_ROOT}/tests-lib/testuser-password.txt";
    # File with testuser password (only one line)
    my $p_f_h;
    # File handle of file with testuser password
    # $tup
    # testuser password from file $p_f (all content of file $p_f)
    # Except the last character

    open $p_f_h, '<', "$p_f"
        or die "File $p_f does not exist";
    local $INPUT_RECORD_SEPARATOR;
    # Enables 'slurp' mode
    $tup = <$p_f_h>;
    # Whole file now here
    close $p_f_h
        or die "File $p_f does not exist";

    chop $tup;
}

sub get_testuser_password {
    my $dbh = DBI->connect(
        "dbi:SQLite:dbname=$ENV{SKAL_ROOT}/db/skaldb.sqlite3",
        q{},
        q{},
        {RaiseError => 1}
    ) or die $DBI::errstr;
    my $sth = $dbh->prepare(
        q{select password from users where name = 'testuser';}
    );
    my $rv  = $sth->execute or die $DBI::errstr;
    die $DBI::errstr if ($rv < 0);
    my @array;
    while (my @arr = $sth->fetchrow_array) {
        push @array, @arr;
    }
    $dbh->disconnect or die $DBI::errstr;
    return $array[0];
}

sub test_get_401 {
    my $t_obj = shift;

    $t_obj->get_ok($_)
          ->status_is(401)
          ->content_type_like(qr'text/html'xms)
          ->content_is('Authentication required!')
        foreach (@_);
    return;
}

sub test_get_401_badauth {
    my $t_obj = shift;

    $t_obj->get_ok($_)
          ->status_is(401)
          ->content_type_like(qr'text/html'xms)
          ->content_is('Wrong username or password!')
        foreach (map {
            (
                "//:\@$_",
                # No username and no password
                "//:skal\@$_",
                # No username and therefore wrong password
                "//user_does_not_exist:\@$_",
                # Wrong username and no password
                "//user_does_not_exist:skal\@$_",
                # Wrong username and therefore wrong password
                "//user+does_not_exist:skal\@$_",
                "//test-user:skal\@$_",
                # Another two wrong usernames
                #   containing non-alphanumeric characters
                #   and therefore wrong passwords
                "//user does_not_exist:skal\@$_",
                "//test user:skal\@$_",
                # Another two wrong usernames
                #   containing spaces
                #   and therefore wrong passwords
                "//test__user:password\@$_",
                # Another wrong username
                #   containing two underscores together
                #   and therefore wrong password
                "//test___user:password\@$_",
                # Another wrong username
                #   containing three underscores together
                #   and therefore wrong password
                "//testuser:password\@$_",
                # Good username and wrong password
            )
        } @_);
    return;
}

1;

